home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13173 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: solon.com!not-for-mail
  2. From: news@Thinkage.On.CA
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
  4. Subject: Re: C coding problem
  5. Date: 4 Apr 1996 18:46:27 -0600
  6. Organization: Thinkage Ltd.
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4k1qh3$5hn@solutions.solon.com>
  10. References: <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com> <4jv6st$crf@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4jv6st$crf@solutions.solon.com> schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
  14. >
  15. >|> a[i] is converted to *(a + i) by the compiler in any case so
  16. >|> whats the advantage with your approach?
  17. >
  18. >The machine doesn't have to add, the machine doesn't have to multiply,
  19. >*a is less complicated to understand than a [i] since only one variable
  20. >is involved, *a is more type safe than a [i], since there is nothing
  21. >constraining i (besides it being of integral type), the optimizer
  22. >has less to do, compile time decreases, etc. 
  23.  
  24. However it is not always so that the machine does a distinct
  25. add or multiply in order to handle a[i].  The "*p++" idiom may look
  26. shot in the C code, and on a PDP-11 it usually resulted in nice
  27. short code.  However, there is no guarentee that this is true of all
  28. hardware.  It is not all that uncommon to have an architecture where
  29. adding 1 to an int is much cheaper than adding one to a pointer,
  30. In such and environment, 
  31.       *q++ = *p++;
  32. loses badly to
  33.       q[i] = p[i], ++i;
  34. if the hardware indexing works in terms of the objects being
  35. referenced.
  36.  
  37. >Why use C if you're uncomfortable with pointer arithmetic?  
  38.  
  39. I don't think the original poster was uncomfortable with pointers.
  40.  
  41. There is a persistent folklore that says just because you code
  42. something with pointers it is magically faster.  It is not true. 
  43. Sometimes using a pointers is faster, sometimes using subscripting is
  44. faster.  Usually it makes MINISCULE difference.
  45.  
  46. The converse folklore is that subscripting is always more readable.
  47. It is also untrue.  Pointer notation can greatly reduce program
  48. clutter so you can see what is happening.
  49.